package LeetCode.LeetCode2;

import java.util.Arrays;

/**
 * @author lumin
 * @date 2022/03/31 16:38
 **/
public class Num33 {
    public static int search(int[] nums, int target) {
        if(nums.length == 1 && nums[0] == target){
            return 0;
        }
        if(nums.length == 1 && nums[0] != target){
            return -1;
        }
        int min = Integer.MAX_VALUE;
        int minIndex = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] < min){
                min = nums[i];
                minIndex = i;
            }
        }
        return Math.max(binarySearch(nums,target,0,minIndex),binarySearch(nums,target,minIndex,nums.length - 1));
    }

    private static int binarySearch(int[] nums, int target, int left, int right) {
        if (nums[left] == target){
            return left;
        }
        if(nums[right] == target){
            return right;
        }
        while(left <= right){
            int mid = (left + right) >> 1;
            if(nums[mid] < target){
                left = mid + 1;
            }
            if(nums[mid] > target){
                right = mid - 1;
            }
            if(nums[mid] == target){
                return mid;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] nums = {3,1};
        System.out.println(search(nums,0));
    }
}